var logLevelsStr = SSN.Launcher.getSetting('loglevels', SSN.Launcher.getConfig('LogLevels', 'ERROR,INFO'))
var memoryLog = "";

window.onerror=function(msg, url, linenumber)
{
	log('ERROR','JavaScript error: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
	return true;
 }

//Debugging output
if( !browser || SSN.Launcher.getSetting('debug')!="undefined" ){
  var debug = function(){
    var p, i=0, l=arguments.length, log=$('<ul>');
    while(i<l){ log.append( $('<li>').html(arguments[i++]) ); }
    $('#logging').append(log);
  };
} else {
  var debug = function(){ };
}

function fillLeft(s,c,n)
{
	while(s.length < n)
		s = c + s;
	return s;
}

function fillRight(s,c,n)
{
	while(s.length < n)
		s =s + c;
	return s;
}

if( browser ){
  log = function(){ arguments[0]="<b>"+arguments[0]+":</b>"; debug.apply(this,arguments); };
} else {
  log = function(type, msg)
  {
      var d = new Date();
      var filename='launcher_'+d.getFullYear() + fillLeft(''+(d.getMonth()+1),'0',2) + fillLeft(''+d.getDate(),'0',2) + '.log';
      var time=d.getFullYear()+fillLeft(''+(d.getMonth()+1),'0',2)+fillLeft(''+d.getDate(),'0',2)+fillLeft(''+d.getHours(),'0',2)+fillLeft(''+d.getMinutes(),'0',2);
      var message = time + ' '+fillRight(type,' ',6)+' '+msg;
      if(logLevelsStr.search(type)>=0)
          document.SSNLauncher.SSNLauncher_ReturnBool("log","string:"+filename+"|string:"+message+"|")
      memoryLog+=message+"\n";
  };
}

function memoryLoggingHandler(time, message)
{
	log('SSNFO',message);
	if((message!=null)&&(message.length > 20))
	{
		if(message.substr(0,16)=='Error retrieving')
		{
			if($("#pchecking").is(":visible"))
				screen.pchecking.hide();
			if(message.indexOf('/'+currentDownloadingProductName+'.packagelist')>0)
			{
				if(!tempRetryPackageList())
				{
					error(strings[lang]["Unable to retrieve patch data."]).show();
				}
			}
		}
		else
		if(message == 'Patcher_RemovePackage([patcher]) successful')
		{
			if($("#pchecking").is(":visible"))
				screen.pchecking.hide();
		}
	}
}

